package com.hns.phone.safety.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.aop.orgTree.DriverInterfaceAnnotation;
import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.aop.sedate.SedateAnnotation;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.master.entity.CommMftAlmEntity;
import com.hns.can.master.service.TendencyChartsDataTools;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.phone.common.vo.PhoneParamsVo;
import com.hns.phone.safety.vo.DriverUnsafeRankingVo;
import com.hns.phone.safety.vo.FaultSummeryVo;
import com.hns.phone.safety.vo.FaultTypeRankingVo;
import com.hns.phone.safety.vo.FaultsDetail4Phone;
import com.hns.phone.safety.vo.RealTimeFailureParamVo;
import com.hns.phone.safety.vo.UnsafeBhvSummeryVo;
import com.hns.phone.safety.vo.VehicleFaultsRankingVo;
import com.hns.tool.pub.PubUtil;

@Service
public class PhoneMaintFaultsService extends CommonService {
	
	@Autowired
    private FillCodeDscValue codeDscValue;
	
	@Autowired
	private TendencyChartsDataTools tendencyChartsDataTools;
	
	/**
	 * 填充关联字段
	 * @param list
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	private void fillCodeDscValue(List<Object> list) throws Exception{
        Map<String,String> map = new HashMap<String,String>();
        map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo fillParamsVo1 = new FillParamsVo("lineId", map);
        map = new HashMap<String,String>();
        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo fillParamsVo2 = new FillParamsVo("organId", map);
        map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo fillParamsVo3 = new FillParamsVo("carId", map);
        codeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_BASE_LINE_INFO, 
        		GlobalConst.CACHE_KEY_BASE_ORGAN, GlobalConst.CACHE_KEY_BASE_CAR_INFO}, 
        		fillParamsVo1, fillParamsVo2, fillParamsVo3);
    }
	
	/**
     * 获取安全管理-车辆安全故障分页数据
     * @param entity
     * @param commonInitParamService
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @LeftOrgAnnotation()
    @Transactional
    @DriverInterfaceAnnotation()
    @SedateAnnotation()
    public Pagination getRealTimeFailure(CommonParamsVo entity, CommonInitParamService commonInitParamService) throws Exception{
        Pagination page = this.myBatisPage("phoneRealTimeFailure.realTimeFailureCount", "phoneRealTimeFailure.getRealTimeFailure", entity, CommMftAlmEntity.class);
        List<Object> list = page.getData();
        fillCodeDscValue(list);
        page.setData(list);
        return page;
    }
    
//    /**
//     * 获取指定故障详情(手机)
//     * @param entity
//     * @param request
//     * @param commonInitParamService
//     * @return
//     * @throws Exception
//     * @author:zhangyu
//     * @email:zhangyu2222z@sina.cn
//     */
//    @SuppressWarnings({ "unchecked", "rawtypes" })
//	@LeftOrgAnnotation()
//    @Transactional
//    //@DriverInterfaceAnnotation()
//    public List<FaultsDetail4Phone> getFaultsDetail4Mobile(CommonParamsVo entity, HttpServletRequest request,
//    		CommonInitParamService commonInitParamService) throws Exception {
//    	//查询结果
//        List list = PubUtil.convertListMapToObject(sqlMapDao.selectList("phoneRealTimeFailure.getFaultsDetail4Mobile", entity), FaultsDetail4Phone.class);
//        fillCodeDscValue(list);
//        Map<String,String> mapTmp = new HashMap<String,String>();
//        mapTmp.put("mftClCode", "MFTC0000");
//        codeDscValue.fillCodeDscValue(list, mapTmp);
//        return list;
//    }
    
    
    /**
	 * 故障趋势（单车）
	 * @param funcType 子功能映射名称(根据不同映射名称执行对应db命令)
	 * @param map 参数
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<T> maintFaultTendency(Map<String, Object> map, String className, String dateType) throws Exception{
		//TODO
		Class<?> clazz = Class.forName("com.hns.phone.safety.vo."+className);
//		List<T> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("phoneRealTimeFailure.getCarAnaTrend", map), clazz);
		List<T> list = null;
		try{
			List tmpList  = tendencyChartsDataTools.fillMultiTendencyData(this.sqlMapDao.selectList("phoneRealTimeFailure.getCarAnaTrend", map), dateType);
			list = PubUtil.convertListMapToObject(tmpList, clazz);
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	
	/**
	 * 车辆故障类型排名
	 * @param entity
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public Pagination getFaultTypeRanking(RealTimeFailureParamVo entity) {
		Pagination page = this.myBatisPage("phoneRealTimeFailure.getFaultTypeRankingCount", "phoneRealTimeFailure.getFaultTypeRanking", entity, FaultTypeRankingVo.class);
		return page;
	}

	/**
	 * 故障趋势（线路）
	 * @param map
	 * @param className
	 * @param dateType
	 * @return
	 * @throws Exception
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("unchecked")
	public List<T> maintLineFaultTendency(Map<String, Object> map, String className, String dateType) throws Exception {
		//TODO
		Class<?> clazz = Class.forName("com.hns.phone.safety.vo."+className);
//		List<T> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("phoneRealTimeFailure.getLineAnaTrend", map), clazz);
		List<T> list = null;
		try{
			List tmpList  = tendencyChartsDataTools.fillMultiTendencyData(this.sqlMapDao.selectList("phoneRealTimeFailure.getLineAnaTrend", map), dateType);
			list = PubUtil.convertListMapToObject(tmpList, clazz);
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 故障统计
	 * @param entity
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public List<FaultSummeryVo> getFaultSummery(PhoneParamsVo entity) {
		List<FaultSummeryVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("phoneRealTimeFailure.getFaultSummery", entity), FaultSummeryVo.class);
		return list;
		
	}

	/**
	 * 车辆故障排名
	 * @param entity
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public Pagination getVehicleFaultsRanking(PhoneParamsVo entity) {
		Pagination page = this.myBatisPage("phoneRealTimeFailure.vehicleFaultsRankingCount", "phoneRealTimeFailure.vehicleFaultsRanking", entity, VehicleFaultsRankingVo.class);
		return page;
	}

	/**
	 * 不安全行为统计
	 * @param entity
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public List<UnsafeBhvSummeryVo> getUnsafeBhvSummery(PhoneParamsVo entity) {
		List<UnsafeBhvSummeryVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("phoneRealTimeFailure.unsafeBhvSummery", entity), UnsafeBhvSummeryVo.class);
		return list;
	}

	/**
	 * 不安全行为排名
	 * @param entity
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public Pagination getDriverUnsafeRanking(PhoneParamsVo entity) {
		Pagination page = this.myBatisPage("phoneRealTimeFailure.driverUnsafeRankingCount", "phoneRealTimeFailure.driverUnsafeRanking", entity, DriverUnsafeRankingVo.class);
		return page;
	}

	/**
	 * 车辆故障排名（前五）
	 * @param entity
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	public List<VehicleFaultsRankingVo> getVehicleFaultsRankingForFive(PhoneParamsVo entity) {
		List<VehicleFaultsRankingVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("phoneRealTimeFailure.vehicleFaultsRankingForFive", entity), VehicleFaultsRankingVo.class);
		return list;
	}

	public List<DriverUnsafeRankingVo> getDriverUnsafeRankingForFive(PhoneParamsVo entity) {
		List<DriverUnsafeRankingVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("phoneRealTimeFailure.unsafeDriverRankingForFive", entity), DriverUnsafeRankingVo.class);
		return list;
	}
	
	
	
}
